<template>
  <a-modal
    v-model:open="show"
    width="40%"
    centered
    :title="title"
    :footer="null"
    @cancel="handleCloseDialog"
  >
    <a-form
      ref="formRef"
      class="form"
      :model="formState"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-item ref="roleName" label="角色名称" name="roleName">
        <a-input
          :bordered="false"
          class="h-form-input"
          v-model:value="formState.roleName"
        />
      </a-form-item>
      <a-form-item ref="roleKey" label="角色值" name="roleKey">
        <a-input
          :bordered="false"
          class="h-form-input"
          v-model:value="formState.roleKey"
        />
      </a-form-item>
      <a-form-item ref="state" label="启用状态" name="state">
        <a-select
          ref="select"
          class="h-form-input"
          :bordered="false"
          v-model:value="formState.state"
          style="width: 100%"
        >
          <a-select-option v-for="item in stateEnum.data" :value="item.value">{{
            item.name
          }}</a-select-option>
          >
        </a-select>
      </a-form-item>
      <a-form-item ref="menuList" label="分配权限" name="parentId">
        <a-tree
          v-model:checkedKeys="formState.menuList"
          checkable
          :tree-data="treeData"
          :field-names="{
            children: 'children',
            title: 'menuName',
            key: 'menuId',
          }"
        >
        </a-tree>
      </a-form-item>
      <a-form-item :wrapper-col="{ offset: 9, span: 4 }">
        <div class="btns center">
          <a-space>
            <a-button @click="handleCloseDialog">取消</a-button>
            <a-button type="primary" @click="handleSubmit">确定保存</a-button>
          </a-space>
        </div>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { reactive, ref, onMounted } from "vue";
import stateEnum from "@/enums/stateEnum.js";
import {
  getMenuList,
  addRole,
  updateRole,
  getRoleMenuList,
} from "@/api/system";
import { message } from "ant-design-vue";
const props = defineProps(["show", "title", "list"]);
const { show, title, list } = props;
const formRef = ref();
const labelCol = {
  span: 3,
};
const wrapperCol = {
  span: 18,
};
const treeData = ref([]);
function getMenuListFn() {
  getMenuList().then((res) => {
    treeData.value = res.data;
  });
}
let formState = reactive({
  roleName: undefined,
  roleKey: undefined,
  state: undefined,
  menuList: [],
});
const rules = {
  roleName: [
    {
      required: true,
      message: "请输入角色名称",
      trigger: "blur",
    },
  ],
  roleKey: [
    {
      required: true,
      message: "请输入角色值",
      trigger: "blur",
    },
  ],
};
const emits = defineEmits(["handleSave", "handleCancel"]);
const handleSubmit = () => {
  if (list && list.roleId) {
    updateRole(formState).then((res) => {
      message.info("修改成功");
      emits("handleSave", true);
    });
  } else {
    addRole(formState).then((res) => {
      message.info("添加成功");
      emits("handleSave", true);
    });
  }
};
const handleCloseDialog = () => {
  emits("handleCancel", false);
};
onMounted(() => {
  getMenuListFn();
  if (list && list.roleId) {
    formState = Object.assign(formState, list);
    formState.state = Number(formState.state)
    getRoleMenuList({ id: list.roleId }).then((res) => {
      if (res.code == 200) {
        formState.menuList = res.data.map((res) => res.menuId);
      }
    });
    console.log(formState);
  }
});
</script>
<style scoped lang="less">
.form {
  margin-top: 24px;
}
.content {
  background: #f4f6f8;
  border-radius: 8px;
  margin-top: 20px;
  min-height: 200px;
  margin-bottom: 40px;
}
.h-form-input {
  background-color: #f3f5f7;
}
</style>
